package com.wushi.emeeting.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.stereotype.Repository;

import com.wushi.emeeting.model.Meeting;

@Repository
public class MeetingDAO {
	public void newMeeting(Meeting meeting){
		Session session=DAOutil.getSession();
		Transaction trans=session.beginTransaction();
		try{
			session.save(meeting);
			trans.commit();
		}catch(Exception e){
			e.printStackTrace();
			trans.rollback();
		}finally{
		session.close();
		}
	}
	
	public void deleteMeeting(Meeting meeting){
		Session session=DAOutil.getSession();
		Transaction trans=session.beginTransaction();
		try{
			session.delete(meeting);
			trans.commit();
		}catch(Exception e){
			e.printStackTrace();
			trans.rollback();
		}finally{
		session.close();
		}
	}
	
	public void updateMeeting(Meeting meeting){
		Session session=DAOutil.getSession();
		Transaction trans=session.beginTransaction();
		try{
			session.saveOrUpdate(meeting);
			trans.commit();
		}catch(Exception e){
			e.printStackTrace();
			trans.rollback();
		}finally{
		session.close();
		}
		
	}
	
	public List<Meeting> getMeetings(){
		Session session=DAOutil.getSession();
		Query query=session.createQuery("from Meeting as m order by m.startDateTime asc");
		List<Meeting> list=(List<Meeting>)query.list();
		session.close();
		return list;
	}
	
}
